import global from '../utils/global'
import { ipcMain } from 'electron'

export function initIpcFollowWindow() {
  ipcMain.on('get-customer-name', () => {
    for (const [_, value] of global.idToShopInfo) {
      if (value.visibilityState == 'visible') {
        value.ws.send(
          JSON.stringify({
            type: 'getCustomerName'
          })
        )
      }
    }
  })

  ipcMain.on('click-answer', (_, name: string) => {
    for (const [_, value] of global.idToShopInfo) {
      let arr = name.split(':')
      if (arr[0] == value.mall_name) {
        value.ws.send(
          JSON.stringify({
            type: 'clickAnswer'
          })
        )
        global.followWindow?.webContents.send('change-loading-text', '自动答题中，请稍后...')
      }
    }
  })

  ipcMain.on('get-customer-service-name', () => {
    for (const [_, value] of global.idToShopInfo) {
      if (value.visibilityState == 'visible') {
        let list = {
          name: `${value.mall_name}:${value.username}`,
          isNeedAnswer: value.isNeedAnswer
        }
        global.followWindow?.webContents.send('customer-service', list)
      }
    }
  })
}
